package com.taxigang.client.uibinder;

import java.util.HashMap;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.taxigang.client.Authenication.Provider;
import com.taxigang.client.Session;
import com.taxigang.client.TaxiGangHandler;
import com.taxigang.client.TaxiGangProcessor;
import com.taxigang.client.TaxigangMode;
import com.taxigang.client.facebook.FacebookApi;
import com.taxigang.client.micro.Hideable;
import com.taxigang.client.ui.ButtonType;
import com.taxigang.client.ui.map.Geolocator;
import com.taxigang.client.ui.map.JoinView;
import com.taxigang.client.ui.map.MapView;
import com.taxigang.client.ui.map.NavigatorManager;
import com.taxigang.client.ui.map.ProcessingDialog;
import com.taxigang.client.ui.map.SearchView;
import com.taxigang.client.ui.map.SuggestionView;
import com.taxigang.shared.RouteInfo;
import com.taxigang.shared.TaxiCompanyInfo;
import com.taxigang.shared.TaxiGangInfo;

public class TaxiGangRoute extends Composite implements MapView, SuggestionView, JoinView {

	private static HelloWidgetWorldUiBinder uiBinder = GWT
	.create(HelloWidgetWorldUiBinder.class);

	interface HelloWidgetWorldUiBinder extends
	UiBinder<Widget, TaxiGangRoute> {
	}

	private HashMap<PanelType,TaxiGangPanel> taxiGangPanels;

	public TaxiGangRoute() {
		initWidget(uiBinder.createAndBindUi(this));
		//mapViewWidget = new NavigatorWidget(searchAndCreate);
		taxiGangPanels = new HashMap<PanelType, TaxiGangPanel>();
		taxiGangPanels.put(PanelType.SEARCH,searchAndCreate);
		taxiGangPanels.put(PanelType.FEEDBACK,feedback);
		taxiGangPanels.put(PanelType.LOGIN,login);
		taxiGangPanels.put(PanelType.CREATE_STEP1,createStep1);
		taxiGangPanels.put(PanelType.CREATE_STEP2A,createStep2a);
		taxiGangPanels.put(PanelType.CREATE_STEP2B,createStep2b);
		taxiGangPanels.put(PanelType.CREATE_STEP3,createStep3);
		taxiGangPanels.put(PanelType.CREATE_STEP4,createStep4);
		taxiGangPanels.put(PanelType.CONGRATULATIONS_CREATE,congratulationsCreate);
		taxiGangPanels.put(PanelType.CONGRATULATIONS_JOIN,congratulationsJoin);
		taxiGangPanels.put(PanelType.JOIN,congratulationsJoin);
		taxiGangPanels.put(PanelType.SORRY,sorry);
		taxiGangPanels.put(PanelType.MYTAXIGANGS,myTaxiGangs);
		taxiGangPanels.put(PanelType.SUGGESTION,suggestion);
		taxiGangPanels.put(PanelType.MESSAGES,messages);
		taxiGangPanels.put(PanelType.NEW_MESSAGE,newMessage);
		taxiGangPanels.put(PanelType.MOBILENUMBER,mobilenumber);
		taxiGangPanels.put(PanelType.WANT_TO_POST,wantToPost);
		taxiGangPanels.put(PanelType.AWARDS,awards);

		for (TaxiGangPanel p : taxiGangPanels.values()){
			p.setTaxiGangRoute(this);
		}


		Command cmd = null;
		final SearchView searchView = getSearchView();
		if (TaxigangMode.PARTNER == Session.getInstance().getMode()){
			Command outerCmd = new Command(){

				@Override
				public void execute() {
					Command innerCmd = new Command(){

						@Override
						public void execute() {
							Command cmdRunGeo = new Command(){

								@Override
								public void execute() {
									Geolocator geolocation = new Geolocator();
									geolocation.runGeolocation(getSearchView(),taxiGangMapPanel);							
								}

							};
							final RouteInfo partnerInfo = Session.getInstance().getRoute();
							NavigatorManager.createDestinationMarker(partnerInfo.getDestination(),searchView, taxiGangMapPanel, true, cmdRunGeo);
							searchView.setMode(SearchView.SearchDisplayMode.DESTINATION);

						}

					};
					String partnerRef = Session.getInstance().getRef();
					TaxiGangHandler.getInstance().getPartner(partnerRef,innerCmd);
				}
			};
			
			NavigatorManager.executeNavigator(outerCmd,searchAndCreate, taxiGangMapPanel);
	
				
			

			} else {
				if (TaxigangMode.NONE == Session.getInstance().getMode() ){

					NavigatorManager.executeNavigator(null,searchAndCreate, taxiGangMapPanel);
					//no partnerm && no suggestion
					Geolocator geolocation = new Geolocator();
					geolocation.runGeolocation(getSearchView(),taxiGangMapPanel);

				} else {
					if (TaxigangMode.ROUTE == Session.getInstance().getMode() ){

						cmd = new Command(){

							@Override
							public void execute() {
								final RouteInfo routeInfo = Session.getInstance().getRoute();

								Command cmdRunGeo = new Command(){

									@Override
									public void execute() {
										NavigatorManager.createStartMarker(routeInfo.getStart(),searchView, taxiGangMapPanel, true);
										searchView.setMode(SearchView.SearchDisplayMode.DESTINATION);
										searchView.setMode(SearchView.SearchDisplayMode.TIME);
										searchView.setMode(SearchView.SearchDisplayMode.SEARCH_CREATE);
									}

								};

								NavigatorManager.createDestinationMarker(routeInfo.getDestination(),searchView, taxiGangMapPanel, true, cmdRunGeo);

							}

						};
						String routeRef = Session.getInstance().getRef();
						TaxiGangHandler.getInstance().getRoute(routeRef,cmd);

					}
				}
			}


		}

		private PanelType panelToShowAtShowLogin = PanelType.SEARCH;

		//private NavigatorWidget mapViewWidget;

		@UiField
		TaxiGangHeader taxiGangHeader;

		@UiField
		SearchAndCreate searchAndCreate;

		@UiField
		Feedback feedback;

		@UiField
		Login login;

		@UiField Sorry sorry;
		@UiField MyTaxiGangs myTaxiGangs;
		@UiField Suggestion suggestion;
		@UiField Messages messages;
		@UiField NewMessage newMessage;

		@UiField CreateStep1 createStep1;
		@UiField CreateStep2a createStep2a;
		@UiField CreateStep2b createStep2b;
		@UiField CreateStep3 createStep3;
		@UiField CreateStep4 createStep4;
		@UiField CongratulationsCreate congratulationsCreate;
		@UiField CongratulationsJoin congratulationsJoin;
		@UiField Mobilenumber mobilenumber;
		@UiField WantToPost wantToPost;
		@UiField Awards awards;


		@UiField
		TaxiGangMapPanel taxiGangMapPanel;


		@Override
		public Hideable getHideable() {
			if (true)
				throw new RuntimeException();
			return null;
		}

		@Override
		public void setButtonMode(ButtonMode buttonMode) {
			if (true)
				throw new RuntimeException();		
		}

		@Override
		public void enableNavigationButtons() {
			Window.alert("TODO:enableNavigationButtons");
			//		int currentTaxiGangIndex = session.getCurrentSuggestionIndex();
			//		int numCurrentTaxiGangs = session.getTaxiGangInfoList().length;
			//		windowWidget.getNextButton().setVisible(currentTaxiGangIndex < numCurrentTaxiGangs-1);		
			//		windowWidget.getPreviousButton().setVisible(currentTaxiGangIndex != 0);
		}

		@Override
		public void showSuggestionInText(TaxiGangInfo suggestion) {
			Window.alert("TODO:showSuggestionInText");

		}

		@Override
		public void resetSuggestion() {
			NavigatorManager.resetSuggestion(searchAndCreate, taxiGangMapPanel);
		}

		@Override
		public void setPanelToShowOnLogin(PanelType panelType) {
			this.panelToShowAtShowLogin = panelType;

		}

		@Override
		public void createStartMarker(String start, boolean setAddress) {
			NavigatorManager.createStartMarker(start, searchAndCreate, taxiGangMapPanel, setAddress);

		}

		@Override
		public void createDestinationMarker(String destination, boolean setAddress) {
			NavigatorManager.createDestinationMarker(destination,searchAndCreate, taxiGangMapPanel, setAddress, null);

		}


		@Override
		public void createStartDestinationMarker(String start, String dest, boolean setAddress) {
			NavigatorManager.createStartDestinationMarker(start, dest,searchAndCreate, taxiGangMapPanel, setAddress);

		}

		@Override
		public void showSuggestionInMap(TaxiGangInfo suggestion) {
			NavigatorManager.createSuggestion(suggestion,searchAndCreate, taxiGangMapPanel, false);
		}

		@Override
		public void showMyTaxiGangInMap(TaxiGangInfo suggestion, boolean setAddress) {
			NavigatorManager.createStartDestinationMarker(suggestion.getStart(),suggestion.getDestination(),searchAndCreate, taxiGangMapPanel, setAddress);

		}

		@Override
		public HasClickHandlers getButton(ButtonType buttonType) {
			//		HasClickHandlers hasClickHandlers=null;
			//		switch (buttonType){
			//		//case Feedback: hasClickHandlers = taxiGangHeader.getFeedbackButton(); break;
			//		//case Sigin: hasClickHandlers = taxiGangHeader.getLoginButton(); break;
			//		default:
			//			throw new RuntimeException("unrecognised:"+buttonType);
			//		}
			//return hasClickHandlers;
			return null;
		}



		@Override
		public void setLoggedOut(String msg) {
			Log.info("setLoggedOut "+msg);
			show(PanelType.LOGIN);
			taxiGangHeader.showLoginButton(true);
		}

		@Override
		public void showLoginButton(boolean show){
			taxiGangHeader.showLoginButton(show);
		}

		@Override
		public void setLoggedIn(Provider provider) {
			if (provider == Provider.Facebook){
				TaxiGangProcessor.signOutLink.addClickHandler(new ClickHandler() {						
					@Override
					public void onClick(ClickEvent event) {
						FacebookApi.fbLogout();							
					}
				});
			}
			if (panelToShowAtShowLogin!=null){
				show(panelToShowAtShowLogin);
			}

			//TODO
			//		if (signOutWidget != null){
			//			signOutWidget.setVisible(true);	//null for FB
			//		}
			//taxiGangHeader.getLoginButton().setVisible(false);

		}

		@Override
		public void setFeedbackButtonVisible(boolean b) {
			Window.alert("TODO:setFeedbackButtonVisible");

		}

		@Override
		public void setLoginButtonVisible(boolean b) {
			Window.alert("TODO:setLoginButtonVisible");

		}

		@Override
		public void showGuide() {
			Window.alert("TODO:showGuide");

		}

		@Override
		public void hideGuide() {
			//Window.alert("TODO:hideGuide");

		}

		@Override
		public void setStartCity() {
			Window.alert("TODO:handle setStartCity");
			//		if (mapViewWidget!=null){
			//			mapViewWidget.setStartCity();	
			//		}
		}

		@Override
		public void showMap() {
			//NavigatorManager.showMap(taxiGangMapPanel);

		}

		@Override
		public void show(PanelType panelType) {

			ProcessingDialog.hide();
			for (TaxiGangPanel p : taxiGangPanels.values()){
				p.hide();
			}
			taxiGangPanels.get(panelType).show();
		}

		@Override
		public SearchView getSearchView() {
			return searchAndCreate;
		}


		public enum CreateMode {CALL_A_TAXI,MEET_AT_RANK}
		private CreateMode createMode;
		public CreateMode getCreateMode() {
			return createMode;
		}
		public void setCreateMode(CreateMode createMode) {
			this.createMode=createMode;
		}

		public void showMenu() {
			taxiGangHeader.showMenu();

		}

		@Override
		public HasClickHandlers getJoinButton() {
			// TODO Auto-generated method stub
			return suggestion.getJoinButton();
		}

		//	@Override
		//	public void initOracle() {
		//		searchAndCreate.initOracle();
		//
		//	}

		@Override
		public void displayTaxiCompanyList(TaxiCompanyInfo[] listTaxiCompanies) {
			createStep2a.setTaxiCompanyList(listTaxiCompanies);
		}

	}
